package ualrcdg.client;

import ualrcdg.shared.CDGXMLHelper;
import ualrcdg.shared.dto.Game;
import ualrcdg.shared.exceptions.CDGException;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class TesterActivity extends Activity implements OnClickListener
{

	int mLayout;
	protected ScriptRenderer sr;
	protected Game g;

	protected Resources r = getResources( );
	protected final String PACKAGE_NAME = getPackageName( );
	
	// Need handler for callbacks to the UI thread
	final Handler mHandler = new Handler( );

	// Create runnable for posting
	final Runnable mUpdateResults = new Runnable( )
	{
		@Override
		public void run( )
		{
			updateResultsInUi( );
		}
	};

	@Override
	public void onCreate( Bundle savedInstanceState )
	{
		super.onCreate( savedInstanceState );

		/* The parseLayout method will determine how many child nodes a layout has for
		 * buttons, edittexts, textviews, layout types, etc.
		 * LayoutParser lp = new LayoutParser();
		 * lp.parseLayout(this);
		 */

		try
		{
			g = CDGXMLHelper.parseGameFromInputStream( getResources( ).openRawResource( R.raw.game ), getResources( ).openRawResource( R.raw.chunk ) );
			sr = new ScriptRenderer( g );
			sr.setScriptId( "script3" );

		}
		catch( CDGException e )
		{
			// TODO Auto-generated catch block
			e.printStackTrace( );
		}
		mLayout = r.getIdentifier( sr.renderScript( "" ), "layout", PACKAGE_NAME );
		setContentView( mLayout );
		final Button button0 = ( Button ) findViewById( R.id.button0 );
		final Button button1 = ( Button ) findViewById( R.id.button1 );
		final Button button2 = ( Button ) findViewById( R.id.button2 );
		button0.setOnClickListener( this );
		button1.setOnClickListener( this );
		button2.setOnClickListener( this );
	}

	@Override
	public void onClick( View v )
	{

		switch( v.getId( ) )
		{
			case R.id.button0:
				if( sr.nextState( "button0" ) == "#END#" )
				{
					finish( );
					System.exit( 0 );
				}
				else
				{
					startLongRunningOperation( "button0" );
					break;
				}

			case R.id.button1:
				if( sr.nextState( "button1" ) == "#END#" )
				{
					finish( );
					System.exit( 0 );
				}
				else
				{
					startLongRunningOperation( "button1" );
					break;
				}

			case R.id.button2:
				if( sr.nextState( "button2" ) == "#END#" )
				{
					finish( );
					System.exit( 0 );
				}
				else
				{
					startLongRunningOperation( "button2" );
					break;

				}
		}
	}

	protected void startLongRunningOperation( final String button )
	{

		// Fire off a thread to do some work that we shouldn't do directly in the UI thread
		Thread t = new Thread( )
		{
			@Override
			public void run( )
			{
				mLayout = r.getIdentifier( sr.renderScript( button ), "layout", PACKAGE_NAME );
				//mText = UsernameField.getText().toString();
				mHandler.post( mUpdateResults );
			}
		};
		t.start( );
	}

	private void updateResultsInUi( )
	{

		// Back in the UI thread -- update our UI elements based on the data in mResults
		//((TextView)findViewById(R.id.TextView)).setText(mText);
		//((EditText)findViewById(R.id.EditUsername)).setText("");
		setContentView( mLayout );
		final Button button0 = ( Button ) findViewById( R.id.button0 );
		final Button button1 = ( Button ) findViewById( R.id.button1 );
		final Button button2 = ( Button ) findViewById( R.id.button2 );
		button0.setOnClickListener( this );
		button1.setOnClickListener( this );
		button2.setOnClickListener( this );
	}
}
